package com.fancy.service.impl;

import com.fancy.dao.FancyMusicUserDao;
import com.fancy.dao.FancyUserListenNumberDao;
import com.fancy.pojo.User;
import com.fancy.pojo.UserListenNumber;
import com.fancy.service.ListenNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ListenNumberServiceImpl implements ListenNumberService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private FancyUserListenNumberDao fancyUserListenNumberDao;
    @Resource
    private FancyMusicUserDao fancyMusicUserDao;

    List<Object> list = new ArrayList<>();
    String keyName = "ranking-";


    @Override
    public boolean ListenNumberAdd(UserListenNumber userListenNumber) {
        Set<String> keys = redisTemplate.keys("ranking-*");
        for (String key : keys
        ) {
            System.out.println(key);
        }
        Integer userId = userListenNumber.getUserId();
        String zsetKey = keyName + userId;
        UserListenNumber zsetValue = userListenNumber;
        if (redisTemplate.opsForZSet().reverseRank(zsetKey, zsetValue) == null)
            redisTemplate.opsForZSet().add(zsetKey, zsetValue, 0.0);
        Double zsetScore = redisTemplate.opsForZSet().score(zsetKey, zsetValue);
        Double aDouble = redisTemplate.opsForZSet().incrementScore(zsetKey, zsetValue, 1);
        return aDouble - zsetScore > 0 ? true : false;
    }

    @Override
    public Set<Object> getListenNumber(UserListenNumber userListenNumber) {
        Integer userId = userListenNumber.getUserId();
        String zsetKey = keyName + userId;
        Set<Object> ranking = (HashSet) redisTemplate.opsForZSet().range(/*zsetKey*/zsetKey, 0, -1);
        return ranking;
    }

    @Override
    public ArrayList<UserListenNumber> getUserByUserId(Integer userId) {
        User user = fancyMusicUserDao.getUserByUserId(userId);
        Integer grade = (int) Math.floor((user.getUserListenQuantity())/10.0);
        return fancyUserListenNumberDao.getListenNumberByUserId(userId);
//        return new ArrayList<>();
    }

    @Override
    public User readDate(Integer userId) {
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
        System.out.println(userId);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
        User user = fancyMusicUserDao.getUserByUserId(userId);
        return user;
    }
}
